<template>
  <view class="index">
    <view class="center">
      <view class="ind_from">
        <view class="row">
          <view class="label">原始浓度(摩尔/升)</view>
          <view class="value">
            <input
              class="input"
              v-model="formData.initialConcentration"
              type="number"
              placeholder="请输入原始浓度"
            />
          </view>
        </view>
        <view class="row">
          <view class="label">原始体积(mL)</view>
          <view class="value">
            <input
              class="input"
              v-model="formData.initialVolume"
              type="number"
              placeholder="请输入原始体积"
            />
          </view>
        </view>
        <view class="row">
          <view class="label">目标浓度(M)</view>
          <view class="value">
            <input
              class="input"
              v-model="formData.finalConcentration"
              type="number"
              placeholder="请输入目标浓度"
            />
          </view>
        </view>
      </view>
      <view class="btn" @click="submit">{{
        isUnlock ? "计算" : "看广告获取计算结果"
      }}</view>
      <view v-if="showResult" class="result">
        <view class="title">需要添加的溶剂体积</view>
        <view class="desc"
          ><text class="strong">{{ result.solventVolume }}(mL)</text></view
        >
      </view>
      <view class="pub_ad_banner">
        <!-- #ifdef MP-TOUTIAO -->
        <ad
          :unit-id="adBannerIdDouyin"
          :ad-intervals="30"
          :fixed="false"
          type="banner"
          scale="100"
        />
        <!-- #endif -->
        <!-- #ifdef MP-KUAISHOU -->
        <ad :type="100011054" :unit-id="adBannerIdKuaishou"></ad>
        <!-- #endif -->
      </view>
    </view>
  </view>
</template>

<script>
import {
  showAdConfirmModal,
  adVideoCreate,
  adVideoShow,
  adBannerIdDouyin,
  adBannerIdKuaishou,
} from "@/common/ad";
export default {
  data() {
    return {
      adBannerIdDouyin: adBannerIdDouyin,
      adBannerIdKuaishou: adBannerIdKuaishou,
      showResult: false,
      isUnlock: false,
      formData: {
        initialConcentration: 5,
        initialVolume: 100,
        finalConcentration: 2,
      },
      result: {
        solventVolume: 0,
      },
    };
  },
  onLoad() {
    adVideoCreate();
  },
  methods: {
    calculateNongdu() {
      let V2 =
        (this.formData.initialConcentration * this.formData.initialVolume) /
        this.formData.finalConcentration;
      console.log("val", V2, this.formData);
      let solventVolume = V2 - this.formData.initialVolume;
      this.result.solventVolume = solventVolume.toFixed(2);
      this.showResult = true;
    },
    getResult() {
      this.calculateNongdu();
      this.isUnlock = true;
    },
    submit() {
      if (this.formData.initialConcentration == "") {
        this.global.toast("请输入原始浓度");
        return;
      }
      if (this.formData.initialVolume == "") {
        this.global.toast("请输入原始体积");
        return;
      }
      if (this.formData.finalConcentration == "") {
        this.global.toast("请输入目标浓度");
        return;
      }
      if (!this.isUnlock) {
        showAdConfirmModal(() => {
          try {
            adVideoShow(() => {
              this.getResult();
            });
          } catch (error) {
            this.getResult();
          }
        });
      } else {
        this.getResult();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.banner {
  display: block;
  width: 100%;
}
.pub_radio {
  position: relative;
  padding-left: 50rpx;
}
.pub_radio:before {
  position: absolute;
  top: 50%;
  margin-top: -20rpx;
  left: 0;
  content: "";
  width: 40rpx;
  height: 40rpx;
  background-image: url("@/static/img/checked.png");
  background-size: cover;
}
.pub_radio.active:before {
  background-image: url("@/static/img/checked_active.png");
  background-size: cover;
}
.ind_from {
  background: #f9f5ef;
  margin-top: 20rpx;
  border: 4rpx solid #b39569;
  border-radius: 10rpx;
  .row {
    padding: 16rpx 20rpx;
    // display: flex;
    border-bottom: 1px solid #ccc;
    &:last-child {
      border-bottom: 0;
    }
    .label {
      width: 100%;
      margin-bottom: 5px;
      flex-shrink: 0;
    }
    .value {
      width: 100%;
      .input {
        width: 100%;
      }
    }
    .pub_radio {
      display: inline-block;
      margin-right: 40rpx;
    }
  }
}
.btn {
  margin-top: 30rpx;
  background: #db0000;
  color: #fff;
  text-align: center;
  padding: 14rpx 0;
  border-radius: 10rpx;
}
.sub_text {
  text-align: center;
  padding-top: 30rpx;
  font-size: 24rpx;
  color: #666;
  .red {
    color: red;
  }
  .bold {
    font-weight: bold;
  }
}
.pub_ad_banner {
  margin-top: 30rpx;
}
.result {
  // display: flex;
  // align-items: center;
  // justify-content: space-between;
  background: #f9f5ef;
  margin-top: 10px;
  padding: 20px 0;

  .left,
  .right {
    width: 100%;
    padding: 10px 10px;
    background: #6f6fff;
    color: #fff;
  }

  .left {
    margin-right: 10px;
  }

  .p {
    display: block;

    .strong {
      font-size: 30px;
      display: inline-block;
      margin-right: 6px;
    }
  }

  .title {
    text-align: center;
    font-size: 30px;
  }
  .desc {
    font-size: 32px;
    text-align: center;
    color: #ff1afe;
  }
}
</style>